package cn.wp.common.core.web;

import cn.wp.common.core.plugin.fileserver.FileServerProperties;
import cn.wp.common.core.web.interceptor.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web Configurer
 * Created by chenyb on 2019/09/24
 */
@Order(-1)
public class WpWebMvcConfigurer implements WebMvcConfigurer {

    private WpInterceptorRegistryProxy interceptorRegistryProxy;

    @Autowired
    private FileServerProperties fileServerProperties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //registry.addResourceHandler("/fileserver/**")
        //        .addResourceLocations(fileServerProperties.getRootFilePath().replaceAll("/","\\\\\\\\") + "\\\\\\\\");

        // 不以http开头说明，说明文件服务托管在本地，添加虚拟路径映射
        if(!fileServerProperties.getRootWebPath().startsWith("http")){
            registry.addResourceHandler( fileServerProperties.getRootWebPath() +"/**")
                    .addResourceLocations("file:" + fileServerProperties.getRootFilePath() + "/");
        }
    }

    /*
    // swagger 资源配置
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }*/

    /*@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedHeaders("*")
                //.allowedOrigins("http://localhost:9000")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE");
    }*/

    // 框架默认拦截器配置
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截器注册代理类
        interceptorRegistryProxy = new WpInterceptorRegistryProxy(registry);

        String[] pathPatterns = interceptorRegistryProxy.getPathPatterns();
        String[] ignores = interceptorRegistryProxy.getIgnores();

        // important 顺序很重要
        interceptorRegistryProxy.addInterceptor(new WebContextInterceptor(pathPatterns,ignores));
        interceptorRegistryProxy.addInterceptor(new ControllerInterceptor(pathPatterns, ignores));
        interceptorRegistryProxy.addInterceptor(new LoginInterceptor(pathPatterns, ignores));
        interceptorRegistryProxy.addInterceptor(new LogInterceptor(pathPatterns, ignores));
        interceptorRegistryProxy.addInterceptor(new ReportInterceptor(pathPatterns, ignores));

        /*interceptorRegistryProxy.addInterceptor(new TokenInterceptor(pathPatterns,ignores));
        interceptorRegistryProxy.addInterceptor(new LoginInterceptor(pathPatterns,ignores));
        interceptorRegistryProxy.addInterceptor(new DbLogInterceptor(pathPatterns,ignores));
        interceptorRegistryProxy.addInterceptor(new DataBossInterceptor(pathPatterns,ignores));
        interceptorRegistryProxy.addInterceptor(new ParamInterceptor(pathPatterns,ignores));
        interceptorRegistryProxy.addInterceptor(new DefaultRenderInterceptor(pathPatterns,ignores));*/
    }

    // 指定路径忽略大小写
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher matcher = new AntPathMatcher();
        matcher.setCaseSensitive(false);
        configurer.setPathMatcher(matcher);
    }

    public WpInterceptorRegistryProxy getInterceptorRegistryProxy() {
        return interceptorRegistryProxy;
    }

}
